﻿using EShopV20.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EShopV20.Areas.Adm.Controllers
{
    public class UserRoleController : Controller
    {
        ApplicationDbContext db = new ApplicationDbContext();
        //
        // GET: /Adm/UserRole/
        public ActionResult Index()
        {
            ViewBag.Users = db.Users;
            ViewBag.Roles = db.Roles;
            return View();
        }

        public ActionResult Update(String RoleId, String UserId)
        {
            var user = db.Users.Find(UserId);
            try
            {
                var userRole = user.Roles.Single(ur => ur.RoleId == RoleId);
                user.Roles.Remove(userRole);
                return Content("Remove");
            }
            catch
            {
                var userRole = new IdentityUserRole
                {
                    RoleId = RoleId,
                    UserId = UserId
                };
                user.Roles.Add(userRole);
                return Content("Add");
            }
            finally
            {
                db.SaveChanges();
            }
        }
	}
}